import java.io.*;
import java.net.*;
import java.util.Date;

public class Client {

    public static void main(String[] pArgs) 
    {
        boolean lStandalone;
    
        if(pArgs.length<2)
        {
            System.out.println("usage: java Client host port [gamekey]");
        }
    
        Player lPlayer=new Player();
        
        try
        {
            Socket lSocket=new Socket(pArgs[0],Integer.parseInt(pArgs[1]));
            PrintWriter lOut=new PrintWriter(lSocket.getOutputStream());
            BufferedReader lIn=new BufferedReader(new InputStreamReader(lSocket.getInputStream()));
    
            if(pArgs.length==2)
            {
                lStandalone=true;
                lOut.println("STANDALONE");
            }
            else
            {
                lStandalone=false;
                lOut.println(pArgs[2]);
            }
            lOut.flush();
    
            //receive the answer
            boolean lFirst; //will be true if we play first
            Date lTime; //time when initialization must be done
            String lLine=lIn.readLine();
            String[] lTokens=lLine.split("[ ]+");
            lFirst=Integer.parseInt(lTokens[1])!=0;
            if(lStandalone)
                lTime=new Date(new Date().getTime()+10000);
            else
                lTime=new Date(Long.parseLong(lTokens[0])/1000);
    
            lPlayer.Initialize(lFirst,new Deadline(lTime));
    
            lOut.println("INIT");
            lOut.flush();
            
            Board lBoard=new Board(lFirst);
            
            while(true)
            {
                lSocket.setSoTimeout(0);
                while(true)
                {
                    lLine=null;
                    try
                    {
                        lLine=lIn.readLine();
                    }
                    catch(Throwable t)
                    {
                    }
                    
                    if(lLine!=null) break;
                }
                int lSpace=lLine.indexOf(' ');
                  if(lStandalone)
                    lTime=new Date(new Date().getTime()+2000);
                else
                    lTime=new Date(Long.parseLong(lLine.substring(0,lSpace))/1000);
                Move lMove=new Move(lLine.substring(lSpace+1),Board.OTHER);
                
                if(lMove.IsEOG())
                {
                    if(lBoard.OwnCount()>lBoard.OtherCount())
                        System.out.println("YOU WIN ("+lBoard.OwnCount()+"-"+lBoard.OtherCount()+")");
                    else if(lBoard.OwnCount()<lBoard.OtherCount())
                        System.out.println("YOU LOSE ("+lBoard.OwnCount()+"-"+lBoard.OtherCount()+")");
                    else
                        System.out.println("DRAW ("+lBoard.OwnCount()+"-"+lBoard.OtherCount()+")");
                    return;
                }
    
                lBoard.DoMove(lMove);
    
                lMove=lPlayer.Play(lBoard,new Deadline(lTime));
                
                lOut.println(lMove.ToString());
                lOut.flush();
                
                if(lMove.IsEOG())
                    return;
                
                lBoard.DoMove(lMove);
            }
        }
        catch(Throwable t)
        {
            t.printStackTrace();
        }
    }
}
